; The Best Damn Sprite Routine Period (XOR Version)
; 
; AUTHOR: Sean McLaughlin (sigma_zk@yahoo.com)
;
; DESCRIPTION:
;	XORs a sprite to PlotSScreen with clipping.
;
; INPUT:
;	D	Abscissa
;	E	Ordinate
;	HL	Pointer to start of sprite data:
;		number of rows; sprite row 1; sprite row 2; et cetera.
; DESTROYS:
;	AF BC DE HL IX
;	Interrupts are disabled
;
.MODULE	_TBDSRP_X
#define	X_PIXEL_SHIFT	.DB $CB,$21,$17		; SLA C\ RLA
ClipSprite_XOR:
	LD	B,(HL)
	LD	A,E
	CP	$80
	JR	C,_PosY
	NEG
	CP	B
	RET	P
	LD	C,A
	NEG
	ADD	A,B
	LD	B,0
	ADD	HL,BC
	LD	E,B
	LD	B,A
_PosY:
	CP	64
	RET	NC
_ClipBtm:
	ADD	A,B
	SUB	65
	JR	C,_DoHoriz
	SUB	B
	CPL
	LD	B,A
_DoHoriz:
	LD	A,D
	CP	$80
	JR	C,_PosX
	CP	-7
	RET	C
	LD	IX,_ClipLeft
	JR	_Patch
_PosX:
	CP	96
	RET	NC
	CP	89
	LD	IX,_ClipRight
	JR	C,_DoSprite
_Patch:
	LD	(IX),0
_DoSprite:
	DI
	INC	HL
	PUSH	HL
	SLA	E
	SLA	E
	LD	H,0
	LD	L,E
	LD	D,H
	ADD	HL,HL
	ADD	HL,DE
	LD	E,A
	SRA	E
	SRA	E
	SRA	E
	JP	P,_Pos
	DEC	D
_Pos:
	ADD	HL,DE
	LD	DE,PlotSScreen
	ADD	HL,DE
	POP	DE
	AND	7
	LD	(_save),SP
	LD	SP,11
	JR	Z,_Aligned
	DEC	A
	LD	C,A
	ADD	A,A
	ADD	A,C
	LD	(_Jump),A
_Loop:
	LD	A,(DE)
	LD	C,A
	XOR	A
_Jump	=$+1
	JR	$
	X_PIXEL_SHIFT
	X_PIXEL_SHIFT
	X_PIXEL_SHIFT
	X_PIXEL_SHIFT
	X_PIXEL_SHIFT
	X_PIXEL_SHIFT
	X_PIXEL_SHIF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